package com.mz.ozy.activity.shop;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.mz.ozy.R;
import com.mz.ozy.activity.home.RedeemVoucherActivity1;
import com.mz.ozy.activity.me.AddressActivity;
import com.mz.ozy.activity.me.PayPwdActivity;
import com.mz.ozy.adapter.ExchangeOrderAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.AddressBean;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.ExchangeOrderBean;
import com.mz.ozy.bean.NetBean;
import com.mz.ozy.bean.OrderBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.presenter.PrivateOrderPresenter;
import com.mz.ozy.mvp.view.PrivateOrderContract;
import com.mz.ozy.utils.PayManager;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.widget.dialog.PayCardDialog;
import com.mz.ozy.widget.dialog.PaySelectDialog;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/11/21.
 */

public class PrivateOrderActivity extends BaseActivity<PrivateOrderContract.View, PrivateOrderPresenter> implements PrivateOrderContract.View {

    private static PrivateDetailActivity detailActivity;
    @BindView(R.id.tv_name)
    TextView mTvName;
    @BindView(R.id.tv_empty)
    TextView mTvEmpty;
    @BindView(R.id.tv_default)
    TextView mTvDefault;
    @BindView(R.id.tv_mobile)
    TextView mTvMobile;
    @BindView(R.id.tv_address)
    TextView mTvAddress;
    @BindView(R.id.rv_goods_list)
    RecyclerView mRvGoodsList;
    @BindView(R.id.tv_goods_count)
    TextView mTvGoodsCount;
    @BindView(R.id.ll_user_info)
    LinearLayout mLlUserInfo;
    @BindView(R.id.tv_freight_count)
    TextView mTvFreightCount;
    @BindView(R.id.tv_real_payment)
    TextView mTvRealPayment;
    @BindView(R.id.iv_firm_right)
    ImageView mIvFirmRight;
    @BindView(R.id.iv_firm_location)
    ImageView mIvFirmLocation;
    @BindView(R.id.btn_submit)
    Button mBtnSubmit;
    @BindView(R.id.et_remark)
    EditText mEtRemark;
    @BindView(R.id.tv_express_delivery)
    TextView mTvExpressDelivery;
    @BindView(R.id.tv_full_reduction)
    TextView mTvReduction;
    private ExchangeOrderAdapter mAdapter;
    private ExchangeOrderBean mFirmOrderBean;
    private int mGoodsNum;
    private double mTotalPrice;
    private double mTotalPriceW;
    private String mGid;//商品id  详情发起的立即购买
    private String mAid;
    private int mArea0id;
    private int mArea1id;
    private int mArea2id;
    private boolean hasAddress = false;
    private boolean hasDefaultAddress = false;
    private double mFreight = 0.00;
    private int payType;
    private String mPrice;
    private String mGname;
    private int mPostion = 0;
    private PayManager mPayManager;
    private PayCardDialog cardDialog;
    private List<ExchangeOrderBean.CardMapBean> mCardList;
    private ExchangeOrderBean.CardMapBean acListBean;

    /**
     * @param context
     * @param firmOrderBean         订单信息
     * @param gid                   商品id
     * @param privateDetailActivity
     */

    public static void start(Context context, ExchangeOrderBean firmOrderBean, String gid, PrivateDetailActivity privateDetailActivity) {
        detailActivity = privateDetailActivity;
        Intent starter = new Intent(context, PrivateOrderActivity.class);
        starter.putExtra("firmOrderBean", firmOrderBean);
        starter.putExtra("gid", gid);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_private_order);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
        initData();
        initTitle();
        init();
        if (hasAddress) {
            initFreight();
            mTotalPriceW = mTotalPrice + mFreight;
        } else {
            mTotalPriceW = mTotalPrice;
            mTvExpressDelivery.setText("运费:¥0.0");
        }
        loadTotalPrice();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        mFirmOrderBean = (ExchangeOrderBean) getIntent().getSerializableExtra("firmOrderBean");
        mGid = getIntent().getStringExtra("gid");
        if (mFirmOrderBean == null) {
            ToastUtils.showToast("数据有误");
            finish();
        }
        mArea0id = mFirmOrderBean.getShaddressMap().getArea0id();
        mArea1id = mFirmOrderBean.getShaddressMap().getArea1id();
        mArea2id = mFirmOrderBean.getShaddressMap().getArea2id();
        if (mArea0id == 0 && mArea1id == 0 && mArea2id == 0) {
            hasAddress = false;
        } else {
            hasAddress = true;
        }
        mPayManager = new PayManager(this);
    }

    private boolean existFreightItem = true;

    private void initFreight() {
        mFreight = 0.00;
        existFreightItem = true;
        mFirmOrderBean.setFreightid(0);
        mFirmOrderBean.setType(1);
        mFirmOrderBean.setAmount("0.00");
        mFirmOrderBean.setContinuation("0.00");
        final List<ExchangeOrderBean.FreightMapBean> freightList = mFirmOrderBean.getFreightList();
        if (freightList != null && freightList.size() > 0) {
            boolean tmpExist = false;
            for (ExchangeOrderBean.FreightMapBean freightMapBean : freightList) {
                boolean isSame = compareFreight(freightMapBean.getArea());
                if (isSame) {
                    tmpExist = true;
                    mFirmOrderBean.setFreightid(freightMapBean.getId());
                    mFirmOrderBean.setType(freightMapBean.getType());
                    mFirmOrderBean.setAmount(freightMapBean.getAmount());
                    mFirmOrderBean.setContinuation(freightMapBean.getContinuation());
                    break;
                }
            }
            if (!tmpExist) {
                existFreightItem = false;
            }
            double tmpFreight = 0.0;
            if (mFirmOrderBean.getType() == 0) {
                tmpFreight = Double.parseDouble(mFirmOrderBean.getAmount());
                if (mGoodsNum > 1) {
                    final int spaceNum = mGoodsNum - 1;
                    tmpFreight = tmpFreight + Double.parseDouble(mFirmOrderBean.getContinuation()) * spaceNum;
                }
            } else if (mFirmOrderBean.getType() == 1) {
                tmpFreight = 0.0;
            } else {
                final String ggweight = mFirmOrderBean.getFreWeight();
                if (TextUtils.isEmpty(ggweight)) {
                    tmpFreight = 0.0;
                } else {
                    double totalWeight = Double.parseDouble(ggweight) * mGoodsNum;
                    if (totalWeight <= 1.0) {
                        tmpFreight = Double.parseDouble(mFirmOrderBean.getAmount());
                    } else {
                        tmpFreight = Double.parseDouble(mFirmOrderBean.getAmount());
                        double spaceWeight = totalWeight - 1.0;
                        final double times = Math.ceil(spaceWeight / 1.0);
                        tmpFreight = tmpFreight + Double.parseDouble(mFirmOrderBean.getContinuation()) * times;
                    }
                }
            }
            mFreight = tmpFreight;
            if (mFreight != 0.00) {
                mTvExpressDelivery.setText("运费 " + "¥" + Utils.subTwoNumber(mFreight));
            } else {
                if (existFreightItem) {
                    mTvExpressDelivery.setText("包邮");
                } else {
                    mTvExpressDelivery.setText("运费 ¥0.0");
                }
            }
        }
    }

    private boolean compareFreight(String area) {
        final String[] areaArr = area.split(",");
        boolean oneSame = false;
        boolean twoSame = false;
        boolean threeSame = false;
        String oneArea = mArea0id + "";
        String twoArea = mArea1id + "";
        String threeArea = mArea2id + "";
        if (areaArr != null && areaArr.length > 0) {
            for (String tmparea : areaArr) {
                if (oneArea.equals(tmparea)) {
                    oneSame = true;
                }
                if (twoArea.equals(tmparea)) {
                    twoSame = true;
                }
                if (threeArea.equals(tmparea)) {
                    threeSame = true;
                }
                if (oneSame == true || twoSame == true || threeSame == true) {
                    return true;
                }
            }
        }
        return false;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(EventBean event) {
        //刷新购物车
        if ((event.getCode() == Constants.EventCode.WX_PAY_SUCCESS || event.getCode() == Constants.EventCode.ALIPAY_PAY_SUCCESS)) {
            ToastUtils.showToast("支付成功");
            EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_BOOK_HOME, null));
            finish();
            if (detailActivity != null) {
                detailActivity.finish();
                detailActivity = null;
            }
        }
        if ((event.getCode() == Constants.EventCode.JUMP_PAY_PWD)) {
            startActivity(new Intent(this, PayPwdActivity.class));
        }
        if (event.getCode() == Constants.EventCode.CHANGE_DEFAULT_ADDRESS) {
            final AddressBean addressBean = (AddressBean) event.getData();
            reLoadAddressMap(addressBean);
            if (mAid.equals("-1")) {
                hasDefaultAddress = true;
                reLoadCurAddress(addressBean);
            } else {
                if (mAid.equals(addressBean.getId())) {
                    mTvDefault.setVisibility(View.VISIBLE);
                } else {
                    mTvDefault.setVisibility(View.GONE);
                }
            }
        }
        if (event.getCode() == Constants.EventCode.EDIT_ADDRESS) {
            final AddressBean addressBean = (AddressBean) event.getData();
            if (addressBean.getId() == mFirmOrderBean.getShaddressMap().getId()) {
                reLoadAddressMap(addressBean);
            }
            reLoadCurAddress(addressBean);
        }
        if (event.getCode() == Constants.EventCode.DELETE_DEFAULT_ADDRESS) {
            hasDefaultAddress = false;
            if (mAid.equals(mFirmOrderBean.getShaddressMap().getId() + "")) {
                reLoadNoAddress();
            }
        } else if (event.getCode() == Constants.EventCode.DELETE_ADDRESS) {
            if (hasDefaultAddress) {
                reLoadDefaultAddress();
            } else {
                reLoadNoAddress();
            }
        }
    }

    private void reLoadNoAddress() {
        mLlUserInfo.setVisibility(View.GONE);
        mTvAddress.setText("");
        mIvFirmLocation.setVisibility(View.GONE);
        mIvFirmRight.setImageResource(R.mipmap.empty_right_arrow);
        mTvEmpty.setVisibility(View.VISIBLE);
        hasAddress = false;
        mAid = "-1";
        if (hasAddress) {
            initFreight();
            mTotalPriceW = mTotalPrice + mFreight;
        } else {
            mTotalPriceW = mTotalPrice;
            mTvExpressDelivery.setText("运费 ¥0.0");
        }
        loadTotalPrice();
    }

    private void reLoadDefaultAddress() {
        mTvName.setText(mFirmOrderBean.getShaddressMap().getConsignee());
        final String contactnumber = mFirmOrderBean.getShaddressMap().getContactnumber();
        final String phonestr = contactnumber.substring(0, 3) + "****" + contactnumber.substring(contactnumber.length() - 4);
        mTvMobile.setText(phonestr);
        mAid = String.valueOf(mFirmOrderBean.getShaddressMap().getId());
        mArea0id = mFirmOrderBean.getShaddressMap().getArea0id();
        mArea1id = mFirmOrderBean.getShaddressMap().getArea1id();
        mArea2id = mFirmOrderBean.getShaddressMap().getArea2id();
        String address = "";
        if (mArea0id == mArea1id) {
            address = mFirmOrderBean.getShaddressMap().getArea1name() + mFirmOrderBean.getShaddressMap().getArea2name() + mFirmOrderBean.getShaddressMap().getXxaddress();
        } else {
            address = mFirmOrderBean.getShaddressMap().getArea0name() + mFirmOrderBean.getShaddressMap().getArea1name() + mFirmOrderBean.getShaddressMap().getArea2name() + mFirmOrderBean.getShaddressMap().getXxaddress();
        }
        mTvAddress.setText(address);
        if (1 == mFirmOrderBean.getShaddressMap().getIsdefault()) {
            mTvDefault.setVisibility(View.VISIBLE);
        } else {
            mTvDefault.setVisibility(View.GONE);
        }
        hasAddress = true;
        initFreight();
        if (!hasAddress) {
            mTotalPriceW = mTotalPrice;
        } else {
            mTotalPriceW = mTotalPrice + mFreight;
        }
        loadTotalPrice();
    }

    private void reLoadCurAddress(AddressBean bean) {
        mLlUserInfo.setVisibility(View.VISIBLE);
        mTvEmpty.setVisibility(View.GONE);
        mIvFirmRight.setImageResource(R.mipmap.firm_right_arrow);
        mIvFirmLocation.setVisibility(View.VISIBLE);
        mTvName.setText(bean.getConsignee());
        final String contactnumber = bean.getContactnumber();
        final String phonestr = contactnumber.substring(0, 3) + "****" + contactnumber.substring(contactnumber.length() - 4);
        mTvMobile.setText(phonestr);
        if (1 == bean.getIsdefault()) {
            mTvDefault.setVisibility(View.VISIBLE);
        } else {
            mTvDefault.setVisibility(View.GONE);
        }
        String address = "";
        if (bean.getArea0id().equals(bean.getArea1id())) {
            address = bean.getArea1name() + bean.getArea2name() + bean.getXxaddress();
        } else {
            address = bean.getArea0name() + bean.getArea1name() + bean.getArea2name() + bean.getXxaddress();
        }
        mTvAddress.setText(address);
        mAid = String.valueOf(bean.getId());
        mArea0id = Integer.parseInt(bean.getArea0id());
        mArea1id = Integer.parseInt(bean.getArea1id());
        mArea2id = Integer.parseInt(bean.getArea2id());
        hasAddress = true;
        initFreight();
        if (!hasAddress) {
            mTotalPriceW = mTotalPrice;
        } else {
            mTotalPriceW = mTotalPrice + mFreight;
        }
        loadTotalPrice();
    }

    private void reLoadAddressMap(AddressBean addressBean) {
        mFirmOrderBean.getShaddressMap().setId(addressBean.getId());
        mFirmOrderBean.getShaddressMap().setArea0id(Integer.parseInt(addressBean.getArea0id()));
        mFirmOrderBean.getShaddressMap().setArea1id(Integer.parseInt(addressBean.getArea1id()));
        mFirmOrderBean.getShaddressMap().setArea2id(Integer.parseInt(addressBean.getArea2id()));
        mFirmOrderBean.getShaddressMap().setArea0name(addressBean.getArea0name());
        mFirmOrderBean.getShaddressMap().setArea1name(addressBean.getArea1name());
        mFirmOrderBean.getShaddressMap().setArea2name(addressBean.getArea2name());
        mFirmOrderBean.getShaddressMap().setXxaddress(addressBean.getXxaddress());
        mFirmOrderBean.getShaddressMap().setContactnumber(addressBean.getContactnumber());
        mFirmOrderBean.getShaddressMap().setConsignee(addressBean.getConsignee());
        mFirmOrderBean.getShaddressMap().setIsdefault(addressBean.getIsdefault());
    }

    @Override
    public void handleFailResponse(NetBean bean) {
        super.handleFailResponse(bean);
        if (Constants.TransCode.TransCode10091.equals(bean.getTransCode())) {
            EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_BOOK_HOME, null));
            finish();
            if (detailActivity != null) {
                detailActivity.finish();
                detailActivity = null;
            }
        }
    }

    @Override
    public void onSubmitSuccess(OrderBean orderBean) {
        showSportsHarmDialog(orderBean);
    }

    @Override
    public void onSubmitError(String msg) {
        ToastUtils.showToast("提交订单失败");
    }

    @Override
    public void onPaySuccess(JSONObject orderBean) {
        if (payType == 0) { //余额支付
            ToastUtils.showToast("支付成功");
            EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_BOOK_HOME, null));
            finish();
            if (detailActivity != null) {
                detailActivity.finish();
                detailActivity = null;
            }
        } else if (payType == 2) {  //微信支付
            mPayManager.handleWxPay(orderBean);
        } else if (payType == 1) {  //支付宝支付
            mPayManager.handleAliPay(orderBean.optString("zfb"));
        }
    }

    @Override
    public void onPayError(String s) {
        EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_BOOK_HOME, null));
        finish();
        if (detailActivity != null) {
            detailActivity.finish();
            detailActivity = null;
        }
    }

    private void init() {
        tvCenterTitle.setText("确认订单");
        LinearLayoutManager llm = new LinearLayoutManager(this);
        mAdapter = new ExchangeOrderAdapter(null);
        mRvGoodsList.setLayoutManager(llm);
        mRvGoodsList.setAdapter(mAdapter);

        mAid = "-1";
        if (TextUtils.isEmpty(mFirmOrderBean.getShaddressMap().getConsignee())) {
            mLlUserInfo.setVisibility(View.GONE);
            mTvAddress.setText("");
            mIvFirmLocation.setVisibility(View.GONE);
            mTvEmpty.setVisibility(View.VISIBLE);
            mIvFirmRight.setImageResource(R.mipmap.empty_right_arrow);
        } else {
            mTvEmpty.setVisibility(View.GONE);
            mIvFirmRight.setVisibility(View.VISIBLE);
            mIvFirmLocation.setVisibility(View.VISIBLE);
            mTvName.setText(mFirmOrderBean.getShaddressMap().getConsignee());
            final String contactnumber = mFirmOrderBean.getShaddressMap().getContactnumber();
            final String phonestr = contactnumber.substring(0, 3) + "****" + contactnumber.substring(contactnumber.length() - 4);
            mTvMobile.setText(phonestr);
            mAid = String.valueOf(mFirmOrderBean.getShaddressMap().getId());

            String address = "";
            if (mFirmOrderBean.getShaddressMap().getArea0id() == mFirmOrderBean.getShaddressMap().getArea1id()) {
                address = mFirmOrderBean.getShaddressMap().getArea1name() + mFirmOrderBean.getShaddressMap().getArea2name() + mFirmOrderBean.getShaddressMap().getXxaddress();
            } else {
                address = mFirmOrderBean.getShaddressMap().getArea0name() + mFirmOrderBean.getShaddressMap().getArea1name() + mFirmOrderBean.getShaddressMap().getArea2name() + mFirmOrderBean.getShaddressMap().getXxaddress();
            }
            mTvAddress.setText(address);

            if (1 == mFirmOrderBean.getShaddressMap().getIsdefault()) {
                mTvDefault.setVisibility(View.VISIBLE);
            } else {
                mTvDefault.setVisibility(View.GONE);
            }
        }
        List<ExchangeOrderBean> carList = new ArrayList<>();
        if (mFirmOrderBean != null) {
            carList.add(mFirmOrderBean);
            mAdapter.setNewData(carList);
        }
        mPrice = mFirmOrderBean.getPrice();
        mGname = mFirmOrderBean.getGname();
        mTotalPrice = Double.parseDouble(mFirmOrderBean.getPrice());
        mGoodsNum = 1;
        initOrderCard();
        //共多少件
        mTvGoodsCount.setText("共" + mGoodsNum + "件  商品金额:");
        mTvFreightCount.setText("¥" + Utils.subTwoNumber(mTotalPrice));
    }

    private void initOrderCard() {
        final List<ExchangeOrderBean.CardMapBean> cardlist = mFirmOrderBean.getCard_list();
        if (cardlist != null && cardlist.size() > 0) {
            this.mCardList = cardlist;
            mPostion = 0;
            acListBean = mCardList.get(0);
            mTvReduction.setText(acListBean.getCardname() + ">");
            mTvReduction.setTextColor(ContextCompat.getColor(this, R.color.c_73b21a));
        } else {
            mTvReduction.setText("无可用卡券>");
        }
    }

    private void loadTotalPrice() {
        if (acListBean != null) {
            mTvRealPayment.setText("¥" + Utils.subTwoNumber(mFreight));
        } else {
            mTvRealPayment.setText("¥" + Utils.subTwoNumber(mTotalPriceW));
        }
    }

    private String getTotalPrice() {
        if (acListBean != null) {
            return Utils.subTwoNumber(mFreight);
        } else {
            return Utils.subTwoNumber(mTotalPriceW);
        }
    }

    @OnClick({R.id.rl_address, R.id.btn_submit, R.id.rl_full_reduction})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_address://地址
                AddressActivity.go(this, 1001, mAid, String.valueOf(mFirmOrderBean.getShaddressMap().getId()));
                break;
            case R.id.rl_full_reduction://地址
//                if (cardDialog != null) {
//                    cardDialog.toggleDialog();
//                } else {
//                    if (mCardList != null && mCardList.size() > 0) {
//                        showPopListView();
//                    }
//                }
                if (mCardList != null && mCardList.size() > 0) {
                    RedeemVoucherActivity1.go(this, mCardList, mPostion, mPrice, mGname);
                } else {
                    ToastUtils.showToast("暂无兑换券");
                }
                break;
            case R.id.btn_submit:
                if ("-1".equals(mAid)) {
                    ToastUtils.showToast("请添加收货地址");
                    return;
                }
                final String remark = mEtRemark.getText().toString().trim();
                String cardid = "";
                if (acListBean != null) {
                    cardid = acListBean.getId() + "";
                }
                mPresenter.detailSubmit(PrivateOrderActivity.this, mGid, mFirmOrderBean.getAttribute_id() + "", mAid, mFirmOrderBean.getFreightid() + "", remark, cardid);
                break;
        }
    }

    private void showPopListView() {
        cardDialog = new PayCardDialog(this);
        cardDialog.setData(mCardList, mCardList.get(0).getId());
        cardDialog.setOnDialogListener(new PayCardDialog.onDialogListener() {
            @Override
            public void onConfirm(ExchangeOrderBean.CardMapBean item) {
                acListBean = item;
                mTvReduction.setText(acListBean.getCardname() + ">");
            }
        });
        cardDialog.toggleDialog();
    }


    private void showSportsHarmDialog(final OrderBean orderBean) {
        String balance = "";
        if (mFirmOrderBean.getBalance_map() != null) {
            if (!TextUtils.isEmpty(mFirmOrderBean.getBalance_map().getBalance())) {
                balance = mFirmOrderBean.getBalance_map().getBalance();
            }
        }
        PaySelectDialog mSportsHarmDialog = new PaySelectDialog(this, 2);
        mSportsHarmDialog.setData(getTotalPrice(), balance);
        mSportsHarmDialog.setOnDialogListener(new PaySelectDialog.onDialogListener() {
            @Override
            public void onConfirm(int type, String payPwd) {
                payType = type;
                mPresenter.onPay(PrivateOrderActivity.this, orderBean.getOrderId(), type + "", payPwd);
            }

            @Override
            public void onCancel() {
                EventBus.getDefault().post(new EventBean(Constants.EventCode.REQUEST_BOOK_HOME, null));
                finish();
                if (detailActivity != null) {
                    detailActivity.finish();
                    detailActivity = null;
                }
            }
        });
        mSportsHarmDialog.toggleDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1001 && resultCode == RESULT_OK && data != null) {
            AddressBean bean = (AddressBean) data.getSerializableExtra("data");
            if (bean == null) {
                ToastUtils.showToast("地址选择失败!");
                return;
            }

            //地址
            mTvName.setText(bean.getConsignee());
            final String contactnumber = bean.getContactnumber();
            final String phonestr = contactnumber.substring(0, 3) + "****" + contactnumber.substring(contactnumber.length() - 4);
            mTvMobile.setText(phonestr);
            if (1 == bean.getIsdefault()) {
                mTvDefault.setVisibility(View.VISIBLE);
            } else {
                mTvDefault.setVisibility(View.GONE);
            }
            mTvEmpty.setVisibility(View.GONE);
            mIvFirmRight.setImageResource(R.mipmap.firm_right_arrow);
            mIvFirmLocation.setVisibility(View.VISIBLE);
            mLlUserInfo.setVisibility(View.VISIBLE);

            String address = "";
            if (bean.getArea0id().equals(bean.getArea1id())) {
                address = bean.getArea1name() + bean.getArea2name() + bean.getXxaddress();
            } else {
                address = bean.getArea0name() + bean.getArea1name() + bean.getArea2name() + bean.getXxaddress();
            }
            mTvAddress.setText(address);
            mAid = String.valueOf(bean.getId());
            mArea0id = Integer.parseInt(bean.getArea0id());
            mArea1id = Integer.parseInt(bean.getArea1id());
            mArea2id = Integer.parseInt(bean.getArea2id());
            hasAddress = true;
            initFreight();
            if (!hasAddress) {
                mTotalPriceW = mTotalPrice;
            } else {
                mTotalPriceW = mTotalPrice + mFreight;
            }
            loadTotalPrice();
        }
        if (requestCode == 1002 && resultCode == RESULT_OK && data != null) {
            mPostion = data.getIntExtra("pos", 0);
            if (mPostion == -1) {
                acListBean = null;
                loadTotalPrice();
                mTvReduction.setText(mCardList.size() + "张可用>");
                mTvReduction.setTextColor(ContextCompat.getColor(this, R.color.c_a5a5a5));
            } else {
                acListBean = mCardList.get(mPostion);
                loadTotalPrice();
                mTvReduction.setText(acListBean.getCardname() + ">");
                mTvReduction.setTextColor(ContextCompat.getColor(this, R.color.c_73b21a));
            }
        }
    }

}
